<?php if
( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->model('users_model');
        $this->load->library('encryption');
        $this->load->helper('file');
        $this->load->helper('url');
    }
    /*登录*/
    public function index()
    {
        //echo "aaaa";
        $this->load->library('form_validation'); // 使用CI的表单验证, 如下:
        $this->form_validation->set_rules('userName', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        /*获取用户的用户名，密码，ip地址*/
        $username = $this->input->post('userName');
        $password = $this->input->post('password');
        $id = $this->input->ip_address();
        /*从ip_address中读到允许访问的ip地址*/
      /*  $ip =  file_get_contents("./ip_address.txt");
        $ip = explode(',',$ip);//把字符串分割到数组中
        $ip = array_flip($ip);//键和值对换
        //print_r($ip);exit;
        // $password = md5($password);*/
        //if (array_key_exists($id,$ip)) {//公司ip122.5.34.178
            if ($this->form_validation->run() !== false) {

                $res = $this->users_model->get_user(
                    $username,
                    $password
                );
                if (!empty($res)) {
                    $this->session->set_userdata('userName', $this->input->post('userName'));
                    $this->session->set_userdata('password', $this->input->post('password'));
                    $this->session->set_userdata('id', $res['id']);

                    redirect('pages/main');
                } else {
                    //  print_r( "ccccccccccc" );exit;
                    $data0['info'] = "用户名或密码错误";
                    $this->load->view('login', $data0);
                }
            }
//        } else {
//            $data0['info'] = "IP限制";
//            $this->load->view('uses/login', $data0);
//        }
    }

    public function logout(){
        unset($_SESSION['userName']);
        unset($_SESSION['id']);
        $data0['info']="";
        $this->load->view('uses/login',$data0);
    }
    public function modifyPassword($id){
        $this->load->helper('url');
        $username=$this->input->post('userName');
        $password=$this->input->post('newPassword');
        //$password = md5($password);
        // $id=$this->input->post('id');
        $data = array(
            'id' => $id,
            'userName' =>$username,
            'password' => $password
        );
        // print_r($data);exit;
        $this->users_model->update_password($data);
        $this->output
            ->set_content_type('application/json')
            ->set_output(json_encode(array('success' => true)));
        return null;
    }
    public function weixin($id){
        $this->load->helper('url');
        $r_name=$this->input->post('r_name');
        $r_id=$this->input->post('r_id');
        $r_ad=$this->input->post('r_ad');
        //$password = md5($password);
        // $id=$this->input->post('id');
        $data = array(
            'id' => $id,
            'userName'=>$this->session->userdata('userName'),
            'password'=>$this->session->userdata('password'),
            'r_name' =>$r_name,
            'r_id' =>$r_id,
            'r_ad'=>$r_ad
        );
        //print_r($data);exit;
        $this->users_model->save_weixin($data);
        $this->output
            ->set_content_type('application/json')
            ->set_output(json_encode(array('success' => true)));
        return null;
    }


}